function [capacity_ofdma, resource_allocation, fig_ofdma] = ofdma_analysis(EbN0_dB, num_users, subcarrier_allocations)
% OFDMA多址接入技术分析
% 输入参数：
%   EbN0_dB - 信噪比范围 (dB)
%   num_users - 用户数量数组
%   subcarrier_allocations - 子载波分配方案
% 输出参数：
%   capacity_ofdma - OFDMA容量
%   resource_allocation - 资源分配结果
%   fig_ofdma - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

% 参数设置
num_subcarriers = 64; % 总子载波数量
subcarrier_spacing = 15e3; % 子载波间隔 (Hz)
channel_gains = rand(num_users, num_subcarriers); % 随机信道增益
noise_power_dBm = -174 + 10*log10(subcarrier_spacing); % 噪声功率
noise_power = 10^((noise_power_dBm-30)/10);

% 初始化结果数组
num_snr = length(EbN0_dB);
num_user_cases = length(num_users);
capacity_ofdma = zeros(num_snr, num_user_cases);
resource_allocation = zeros(num_user_cases, num_subcarriers);

fprintf('OFDMA多址接入技术分析...\n');

% OFDMA容量和资源分配分析
for snr_idx = 1:num_snr
    snr_linear = 10^(EbN0_dB(snr_idx)/10);
    
    for user_idx = 1:num_user_cases
        K = num_users(user_idx);
        
        % 子载波分配 (基于信道增益)
        [~, best_user_per_subcarrier] = max(channel_gains(1:K, :), [], 1);
        
        % 计算每用户容量
        user_capacities = zeros(K, 1);
        for k = 1:K
            % 分配给该用户的子载波
            assigned_subcarriers = find(best_user_per_subcarrier == k);
            num_assigned = length(assigned_subcarriers);
            
            if num_assigned > 0
                % 计算该用户的总容量
                for sc = 1:num_assigned
                    channel_gain = channel_gains(k, assigned_subcarriers(sc));
                    subcarrier_snr = snr_linear * channel_gain / noise_power;
                    
                    if subcarrier_snr > 0
                        user_capacities(k) = user_capacities(k) + ...
                                           subcarrier_spacing * log2(1 + subcarrier_snr);
                    end
                end
            end
        end
        
        % 总容量
        capacity_ofdma(snr_idx, user_idx) = sum(user_capacities);
        
        % 保存资源分配结果 (最后一次迭代)
        if snr_idx == num_snr
            resource_allocation(user_idx, :) = best_user_per_subcarrier';
        end
    end
end

%% 可视化结果
fig_ofdma = figure('Name', 'OFDMA多址接入技术分析', 'Position', [400, 100, 1200, 800]);

% 子图1: OFDMA容量 vs SNR
subplot(2, 2, 1);
for user_idx = 1:num_user_cases
    plot(EbN0_dB, capacity_ofdma(:, user_idx)/1e6, 'LineWidth', 2, 'Color', colors(user_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title('OFDMA容量 vs 信噪比');
legend(arrayfun(@(x) sprintf('%d用户', x), num_users, 'UniformOutput', false), 'Location', 'NorthWest');

% 子图2: 子载波分配
subplot(2, 2, 2);
user_fixed_idx = min(3, num_user_cases); % 选择中等用户数量
allocation_matrix = zeros(K, num_subcarriers);
for k = 1:K
    assigned_subcarriers = find(resource_allocation(user_fixed_idx, :)' == k);
    if ~isempty(assigned_subcarriers)
        allocation_matrix(k, assigned_subcarriers) = 1;
    end
end

imagesc(allocation_matrix);
colorbar;
xlabel('子载波索引');
ylabel('用户索引');
title(sprintf('OFDMA子载波分配 (%d用户)', num_users(user_fixed_idx)));

% 子图3: 频谱效率 vs 用户数量
subplot(2, 2, 3);
snr_index = round(num_snr/2);
spectral_efficiency = capacity_ofdma(snr_index, :) / (num_subcarriers * subcarrier_spacing);
plot(num_users, spectral_efficiency, 's-', 'LineWidth', 2, 'Color', colors(2, :));
grid on;
xlabel('用户数量');
ylabel('频谱效率 (bps/Hz)');
title(sprintf('OFDMA频谱效率 vs 用户数量 (Eb/N0 = %d dB)', EbN0_dB(snr_index)));

% 子图4: 多用户分集增益
subplot(2, 2, 4);
% 计算多用户分集增益
multiuser_diversity_gain = zeros(num_user_cases, 1);
for user_idx = 1:num_user_cases
    K = num_users(user_idx);
    % 多用户分集增益 (相对于单用户)
    if user_idx > 1
        multiuser_diversity_gain(user_idx) = 10*log10(capacity_ofdma(snr_index, user_idx) / ...
                                                      capacity_ofdma(snr_index, 1));
    else
        multiuser_diversity_gain(user_idx) = 0;
    end
end

bar(num_users, multiuser_diversity_gain, 'FaceColor', colors(3, :));
xlabel('用户数量');
ylabel('多用户分集增益 (dB)');
title('OFDMA多用户分集增益');

fprintf('OFDMA分析完成！\n');

end